package cn.tedu.collcetion;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author ：Dsi
 * @date ：2021/11/17 15:50
 * Describe ：该类用于测试集合接口
 */
public class TestCollection {
    public static void main(String[] args) {
        Collection<Integer> c = new ArrayList();
        c.add(45);
        c.add(435);
        c.add(4435);
        c.add(443655);
        c.add(4455);
        System.out.println(c);

//        c.clear();
//        System.out.println(c);
        System.out.println(c.hashCode());//打印集合中的哈希码值
        System.out.println(c.equals(45));//集合和集合中的某一个元素
        System.out.println(c);

        c.contains(200);
        c.add(300);
        System.out.println(c);
        System.out.println(c.isEmpty());
        c.remove(200);
        System.out.println(c);
        System.out.println(c.size());
        Object[] objects = c.toArray();
        System.out.println(Arrays.toString(objects));

        Collection<Integer> c1=new ArrayList();
        c1.add(456);
        c1.add(46);
        c1.add(56);
        c1.add(45);
        c1.add(496);
        c1.add(96);
        System.out.println(c1);

        c.addAll(c1);
        System.out.println(c);
        System.out.println(c1);
        System.out.println(c.containsAll(c1));//ture,判断c中是否含有c1中的所有元素
        c.remove(96);
        System.out.println(c.containsAll(c1));//false
        System.out.println(c.removeAll(c1));//在c中删除c1中的所含有的所有元素
        System.out.println(c);

        System.out.println(c.retainAll(c1));//取c、c1的交集

        System.out.println();
        Collection<Integer> b1=new ArrayList<>();
        b1.add(4);
        Collection<Integer> b2=new ArrayList<>();
        b2.add(4);
        b2.add(5);
        b1.retainAll(b2);
        System.out.println(b1.retainAll(b2));
        System.out.println();

        /*迭代步骤：
        * 1.获取迭代器
        * 2.通过迭代器获取集合中是否还有下一个元素可以迭代
        * 3.获取当前迭代到的元素*/
        Iterator<Integer> i = c1.iterator();
        while (i.hasNext()){
            System.out.println(i.next());
        }

    }
}
